/**
 * 标准的01背包
 * 不滚只对90%，why
 * 因为最前面一段忘记拷贝了
 * 还是直接滚好
 */
#include <bits/stdc++.h>
using namespace std;

using llt = long long;
using pii = pair<int, int>;
using vpii = vector<pii>;
using vi = vector<int>;
using vll = vector<llt>;

llt const INF = 0x1F2F3F4F5F6F7F8F;
llt const NINF = -INF;
llt const MOD = 998244353LL;

int MaxV;
int N;
vector<llt> Cost, W;
// vector<vector<llt>> D;
vector<llt> D;

void work(){
    cin >> MaxV >> N;
    Cost.assign(N + 1, 0);
    W.assign(N + 1, 0);
    for(int i=1;i<=N;++i) cin >> Cost[i] >> W[i];

    // D.assign(N + 1, vector<llt>(MaxV + 1, 0));
    D.assign(MaxV + 1, 0);
    for(int i=1;i<=N;++i){
        // const auto & p = D[i - 1];
        auto & d = D;
        for(int j=MaxV;j>=Cost[i];--j){
            d[j] = max(d[j], d[j - Cost[i]] + Cost[i] * W[i]);
        }
    }
    cout << D[MaxV] << endl;
    return;
}


int main(){
#ifndef ONLINE_JUDGE
    freopen("z.txt", "r", stdin);
#endif
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int nofkase = 1;
    // cin >> nofkase; 
    while(nofkase--) work();    
    return 0;
}
